package com.mall.product.service.impl;

import com.mall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.Query;

import com.mall.product.dao.CategoryDao;
import com.mall.product.entity.CategoryEntity;
import com.mall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> getProductCategoryTree() {
        List<CategoryEntity> categoryList = baseMapper.selectList(null);
        return categoryList.stream()
                .filter(categoryEntity -> 0L == categoryEntity.getParentCid())
                .map((categoryEntity) -> {
                    categoryEntity.setChildren(getTreeListChildren(categoryEntity,categoryList));
                    return categoryEntity;
                })
                .sorted(Comparator.comparingInt(o -> (o.getSort() == null ? 0 : o.getSort())))
                .collect(Collectors.toList());
    }

    /**
     * @Transactional 注解表示为此方法执行多次更新需要增加事务
     * @param category
     */
    @Transactional
    @Override
    public void updateCategoryAndOther(CategoryEntity category) {
        this.updateById(category);
        if(StringUtils.hasText(category.getName())){
            categoryBrandRelationService.updateDetail(category.getCatId(),category.getName());
        }
    }
    /**
     * 递归方法查询子分类
     */
    private List<CategoryEntity> getTreeListChildren(CategoryEntity category,List<CategoryEntity> allCategoryEntity){
        return allCategoryEntity.stream()
                .filter(categoryEntity -> Objects.equals(category.getCatId(), categoryEntity.getParentCid()))
                .peek((categoryEntity) -> categoryEntity.setChildren(getTreeListChildren(categoryEntity,allCategoryEntity)))
                .sorted(Comparator.comparingInt(o -> (o.getSort() == null ? 0 : o.getSort())))
                .collect(Collectors.toList());
    }

    /**
     * 根据子菜单ID获取整个路径数组
     * @param childId
     * @return
     */
    @Override
    public Long[] getAttrPath(Long childId) {
        ArrayList<Long> longs = new ArrayList<>();
        List<Long> attrGroupPath = this.getAttrGroupPath(longs, childId);
        Collections.reverse(attrGroupPath);
        return attrGroupPath.toArray(new Long[0]);
    }

    private List<Long> getAttrGroupPath(List<Long> attrGroupPathList, Long catId) {
        if (catId != 0) {
            CategoryEntity category = this.getById(catId);
            attrGroupPathList.add(category.getCatId());
            getAttrGroupPath(attrGroupPathList, category.getParentCid());
        }
        return attrGroupPathList;
    }
}